package com.didd.core.sensitive.util;

import cn.hutool.core.util.StrUtil;
import com.didd.core.sensitive.enums.SensitiveEnumInterface;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author diddd
 * @date 2021/12/6 9:57 上午
 */
public class SensitiveInfoPatternUtils {


    /**
     * 通过枚举替换
     *
     * @param sour
     * @param enumInterface
     * @return
     */
    public static String replace(String sour, SensitiveEnumInterface enumInterface) {

        Pattern compile = Pattern.compile(enumInterface.getPattern());

        //实例化 Matcher 类
        Matcher matcher = compile.matcher(sour);

        return matcher.replaceAll(enumInterface.getReplaceChar());
    }


    /**
     * @param sour    替换原数据
     * @param pattern 正则公式
     * @param replace 替换后字符
     * @return
     */
    public static String replace(String sour, String pattern, String replace) {

        if (StrUtil.isBlank(pattern) || StrUtil.isBlank(replace)) {
            return sour;
        }
        //获取正则
        Pattern compile = Pattern.compile(pattern);

        //实例化 Matcher 类
        Matcher matcher = compile.matcher(sour);

        return matcher.replaceAll(replace);
    }

}
